from django.db import models
from microerp.apps.base.models import Users

class Product(models.Model):
	id = models.AutoField(primary_key=True)	#产品id 主键
	user = models.ForeignKey(Users,on_delete=models.CASCADE) #外键 对应产品所属的用户
	code = models.CharField(max_length=20)	#产品编号
	name = models.CharField(max_length=20)	#产品名称
	kind = models.IntegerField()				#产品类型
	img = models.CharField(max_length=100)	#产品图片
	state = models.IntegerField()				#产品状态(0-无库存，1-正常，2-禁用)
	inventory = models.FloatField()			#产品库存
	cost = models.FloatField()				#产品进价
	price = models.FloatField()				#建议售价
	producer = models.CharField(max_length=100)#产地
	notes = models.TextField(max_length=1000)	#产品说明
	add_date = models.DateField()             #添加日期


	def introduce(self):
		if len(self.notes) > 45:
			return self.notes[0:40] + "..."
		return self.notes

	def as_json(self):
		return dict(id=self.id, code=self.code, name=self.name,
			inventory=self.inventory,price=self.price,
			cost=self.cost)
